package com.example.im.common

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import androidx.room.Room

class ImApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        context = applicationContext

        val sp = getSharedPreferences(Constants.ACCOUNT_ATTRIBUTES, Context.MODE_PRIVATE)

        val userName = sp.getString(Constants.ACCOUNT_USER_NAME, null)
        val password = sp.getString(Constants.ACCOUNT_PASSWORDS, null)

        if (userName != null && password != null) {
            appDatabase = Room.databaseBuilder(this, AppDatabase::class.java, "$userName.db")
                .allowMainThreadQueries()
                .build()
        }
    }

    companion object {

        @SuppressLint("StaticFieldLeak")
        var context: Context? = null

        fun context(): Context = context!!

        var appDatabase: AppDatabase? = null

        fun appDatabase(): AppDatabase = appDatabase!!
    }
}
